package com.yanqu.road.dao.impl.activity.busyfarming;

        import com.alibaba.fastjson.JSON;
        import com.alibaba.fastjson.TypeReference;
        import com.yanqu.road.dao.db.DbNameStrategy;
        import com.yanqu.road.dao.db.DbParameter;
        import com.yanqu.road.dao.impl.TempDataObjectDao;
        import com.yanqu.road.entity.activity.busyfarming.BusyFarmingStolenRecord;
        import com.yanqu.road.entity.player.UserBaseInfo;
        import com.yanqu.road.utils.string.StringUtils;

        import java.sql.CallableStatement;
        import java.sql.ResultSet;
        import java.sql.SQLException;
        import java.sql.Types;
        import java.util.ArrayList;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;
        import java.util.concurrent.ConcurrentHashMap;

public class BusyFarmingStolenRecordDaoImpl extends TempDataObjectDao<BusyFarmingStolenRecord> {


    public long getMaxUserStolenDbId(int activityId) {
        String sql = "select ifnull(max(dbId),0) as maxId from t_u_activity_busy_farming_stolen_record where activityId=?";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }


    public static String getBusyFarmingStolenRecordSql = "select * from t_u_activity_busy_farming_stolen_record where activityId=? and del=0";
    public Map<Long, List<BusyFarmingStolenRecord>> getBusyFarmingStolenRecord(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<BusyFarmingStolenRecord>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getBusyFarmingStolenRecordSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                BusyFarmingStolenRecord data = parse(rs);
                if(data.isDel()){
                    continue;
                }
                if(!result.containsKey(data.getUserId())){
                    result.put(data.getUserId(), new ArrayList<>());
                }
                result.get(data.getUserId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertBusyFarmingStolenRecordSql = "insert into t_u_activity_busy_farming_stolen_record(dbId,userId,activityId,enemyId,enemyServerId,loss,params,userBaseInfo,stolenTime,del,readed) values (?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(BusyFarmingStolenRecord data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getDbId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnemyId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnemyServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getLoss()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getParams(), "@#$")));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBaseInfoJsonStr()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStolenTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDel()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReaded()));
        if (getDb().executeNoneQuery(insertBusyFarmingStolenRecordSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String updateBusyFarmingStolenRecordSql = "update t_u_activity_busy_farming_stolen_record set enemyId=?,enemyServerId=?,loss=?,params=?,userBaseInfo=?,stolenTime=?,del=?,readed=? where activityId=? and dbId=? and userId=? ";

    @Override
    protected boolean updateData(BusyFarmingStolenRecord data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnemyId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnemyServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getLoss()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getParams(), "@#$")));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBaseInfoJsonStr()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStolenTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDel()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReaded()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDbId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateBusyFarmingStolenRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected BusyFarmingStolenRecord parseData(ResultSet rs) throws SQLException {
        BusyFarmingStolenRecord busyFarmingStolenRecord = new BusyFarmingStolenRecord();
        busyFarmingStolenRecord.setDbId(rs.getLong("dbId"));
        busyFarmingStolenRecord.setUserId(rs.getLong("userId"));
        busyFarmingStolenRecord.setActivityId(rs.getInt("activityId"));
        busyFarmingStolenRecord.setEnemyId(rs.getLong("enemyId"));
        busyFarmingStolenRecord.setEnemyServerId(rs.getLong("enemyServerId"));
        busyFarmingStolenRecord.setLoss(rs.getString("loss"));
        busyFarmingStolenRecord.setParams(StringUtils.stringToStringList(rs.getString("params"), "@#\\$"));
        String baseInfoStr = rs.getString("userBaseInfo");
        if(!StringUtils.isNullOrEmpty(baseInfoStr)){
            UserBaseInfo userBaseInfo = JSON.parseObject(baseInfoStr, new TypeReference<UserBaseInfo>() {});
            if(null != userBaseInfo) {
                busyFarmingStolenRecord.setUserBaseInfo(userBaseInfo);
            }
        }
        busyFarmingStolenRecord.setStolenTime(rs.getInt("stolenTime"));
        busyFarmingStolenRecord.setDel(rs.getBoolean("del"));
        busyFarmingStolenRecord.setReaded(rs.getBoolean("readed"));
        return busyFarmingStolenRecord;
    }
}
